# -*- coding: utf-8 -*-
from openerp.osv import fields
from openerp.osv import osv


class apply_delivery_unlock_wizard(osv.osv_memory):
    _name = 'apply.delivery.unlock.wizard'
    _columns = {
        'message': fields.text(u'信息')
    }

    _defaults = {
        'message': lambda self, cr, uid, context=None: '本次选中 %s 条' % len(context.get('active_ids'))
    }

    def unlock_apply_delivery(self, cr, uid, ids, context=None):

        picking_obj = self.pool.get('stock.picking')
        for apply_row in self.pool.get('apply.delivery').browse(cr, uid, context.get('active_ids'), context=context):
            if not apply_row.is_shipping and apply_row.is_stock_reserved:
                picking_id = picking_obj.search(cr, uid, [('so_id', '=', apply_row.so_id.id),
                                                          ('state', '=', 'assigned')], context=context)
                picking_obj.do_unreserve(cr, uid, picking_id, context=context)
                apply_row.write({'is_stock_reserved': False})
        return True


